package grsu.epam.airline.service;

import grsu.epam.airline.DAO.IFlightDAO;
import grsu.epam.airline.DAOImpl.DAOFactory;
import grsu.epam.airline.constant.Constant;
import grsu.epam.airline.model.Flight;

import java.sql.SQLException;
import java.util.ArrayList;

public class FlightService {
	public ArrayList<Flight> getAllFlight() {
		IFlightDAO flightDao = (IFlightDAO) DAOFactory.getInstance().getDAO(
				Constant.FlightDAO);
		ArrayList<Flight> flights = null;

		try {
			flights = (ArrayList<Flight>) flightDao.getAllFlight();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(flightDao.getConnection());
		}
		return flights;
	}

	public void deleteFlight(int flightId) {
		IFlightDAO flightDao = (IFlightDAO) DAOFactory.getInstance().getDAO(
				Constant.FlightDAO);
		try {
			flightDao.deleteFlight(flightId);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(flightDao.getConnection());
		}
	}

	public Flight getFlightById(int flightId) {
		IFlightDAO flightDao = (IFlightDAO) DAOFactory.getInstance().getDAO(
				Constant.FlightDAO);
		Flight flight = null;

		try {
			flight = (Flight) flightDao.getFlightById(flightId);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(flightDao.getConnection());
		}
		return flight;
	}

	public void updateFlight(int flightId, int crewId, int planeId) {
		IFlightDAO flightDao = (IFlightDAO) DAOFactory.getInstance().getDAO(
				Constant.FlightDAO);
		try {
			flightDao.updateFlight(flightId, crewId, planeId);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(flightDao.getConnection());
		}
	}

	public void addFlight(int crewId, int planeId) {
		IFlightDAO flightDao = (IFlightDAO) DAOFactory.getInstance().getDAO(
				Constant.FlightDAO);
		try {
			flightDao.addFlight(crewId, planeId);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(flightDao.getConnection());
		}
	}

}
